package cloud.dandanit.itservice.service.impl;

import cloud.dandanit.itservice.entity.StatisticsAnalysis;
import cloud.dandanit.itservice.mapper.StatisticalAnalysisMapper;
import cloud.dandanit.itservice.service.StatisticsAnalysisService;
import cloud.dandanit.itservice.service.record.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author luokai
 * @since 2022-06-25
 */
@SuppressWarnings("AlibabaCollectionInitShouldAssignCapacity")
@Service
public class StatisticsAnalysisServiceImpl extends ServiceImpl<StatisticalAnalysisMapper, StatisticsAnalysis> implements StatisticsAnalysisService {

    @Autowired
    private ArticleRecordService articleRecordService;

    @Autowired
    private GiveLikeRecordService giveLikeRecordService;

    @Autowired
    private RateRecordService rateRecordService;

    @Autowired
    private VideoRecordService videoRecordService;

    @Autowired
    private ViewRecordService viewRecordService;

    /**
     * 统计某一天的注册人数
     * @param day 某一天
     */
    @Override
    public void createStatisticsByDay(String day) {

        //删除相同日期的对象，保证同一天不会出现多条数据
        LambdaQueryWrapper<StatisticsAnalysis> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StatisticsAnalysis::getDateCalculated, day);
        this.remove(queryWrapper);

        //获取统计信息
        Integer viewSiteNum = viewRecordService.statisticsData(day);
        Integer rateNum = rateRecordService.statisticsData(day);
        Integer giveLikeNum = giveLikeRecordService.statisticsData(day);
        Integer videoViewNum = videoRecordService.statisticsData(day);
        Integer articleReadNum = articleRecordService.statisticsData(day);

        //创建统计对象
        StatisticsAnalysis daily = new StatisticsAnalysis();

        daily.setViewSiteNum(viewSiteNum);
        daily.setRateNum(rateNum);
        daily.setGiveLikeNum(giveLikeNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setArticleReadNum(articleReadNum);
        daily.setDateCalculated(day);

        this.save(daily);
    }

    /**
     * 图表显示，返回两部分数据，日期json数组，数量json数组
     * @param type 选择查看的类型
     * @param begin 开始日期
     * @param end 结束日期
     * @return 日期json数组+数量json数组
     */
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {

        QueryWrapper<StatisticsAnalysis> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated",type);

        List<StatisticsAnalysis> staList = this.list(wrapper);
        //返回两部分数据，日期json数组，数量json数组
        //后端list <==> 前端数组
        //创建两个list，日期list，数量list
        List<String> dateCalculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();
        for (StatisticsAnalysis sta : staList) {
            //封装日期list
            dateCalculatedList.add(sta.getDateCalculated());
            switch (type) {
                case "view_site_num":
                    numDataList.add(sta.getViewSiteNum());
                    break;
                case "rate_num":
                    numDataList.add(sta.getRateNum());
                    break;
                case "give_like_num":
                    numDataList.add(sta.getGiveLikeNum());
                    break;
                case "video_view_num":
                    numDataList.add(sta.getVideoViewNum());
                    break;
                case "article_read_num":
                    numDataList.add(sta.getArticleReadNum());
                    break;
                default:
                    break;
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("date_calculated_list",dateCalculatedList);
        map.put("num_data_list",numDataList);

        return map;
    }

    /**
     * 统计指定时间段的所有字段的人数接口
     * @param begin 开始日期
     * @param end 结束时间
     * @return 统计列表
     */
    @Override
    public List<Object> showDataByIndex(String begin, String end) {

        QueryWrapper<StatisticsAnalysis> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated","view_site_num","rate_num","give_like_num","article_read_num","video_view_num");

        List<StatisticsAnalysis> staList = this.list(wrapper);

        List<Object> result = new ArrayList<>();
        List<Object> tempViewSiteNum = new ArrayList<>();
        List<Object> tempRateNum = new ArrayList<>();
        List<Object> tempGiveLikeNum = new ArrayList<>();
        List<Object> tempVideoViewNum = new ArrayList<>();
        List<Object> tempArticleReadNum = new ArrayList<>();
        List<Object> tempDateCalculated = new ArrayList<>();

        for (StatisticsAnalysis analysis : staList) {
            tempViewSiteNum.add(analysis.getViewSiteNum());
            tempRateNum.add(analysis.getRateNum());
            tempGiveLikeNum.add(analysis.getGiveLikeNum());
            tempVideoViewNum.add(analysis.getVideoViewNum());
            tempArticleReadNum.add(analysis.getArticleReadNum());
            tempDateCalculated.add(analysis.getDateCalculated());
        }

        result.add(tempViewSiteNum);
        result.add(tempRateNum);
        result.add(tempGiveLikeNum);
        result.add(tempVideoViewNum);
        result.add(tempArticleReadNum);
        result.add(tempDateCalculated);

        return result;
    }
}
